/**
 *  This file is part of AugeasWH.
 *
 *  AugeasWH. is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  AugeasWH. is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with AugeasWH.  If not, see <http://www.gnu.org/licenses/>.
 */

package cz.fit.vutbr.pis2012.augeaswh.control.ekonom.subscriber;

import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;

import cz.fit.vutbr.pis2012.augeaswh.managers.SubscriberManager;
import cz.fit.vutbr.pis2012.augeaswh.persistence.Subscriber;

@ManagedBean
public class SubscriberConverter implements Converter {

	@EJB
	private SubscriberManager sm;
	
	@Override
	public Object getAsObject(FacesContext fc, UIComponent component, String value) {
		return sm.getSupplierByID(Long.valueOf(value));
	}

	@Override
	public String getAsString(FacesContext fc, UIComponent component, Object value) {
		Subscriber subscriber = (Subscriber)value;
		if (subscriber == null || subscriber.getId() == null)
			return "";
		return subscriber.getId().toString();
	}
}
